package net.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.admin.db.item.CustBean;
import net.admin.db.item.CustDAO;

public class CustRegistAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActionForward af = new ActionForward();
		CustBean crb = new CustBean();
		CustDAO dao = new CustDAO();
		
		HttpSession session = request.getSession();
		String admin_id=(String)session.getAttribute("user_id");
		String cust_nm = request.getParameter("cust_nm");
		String ceo_nm = request.getParameter("ceo_nm");
		String corp_no = request.getParameter("corp_no");
		String cust_tel = request.getParameter("cust_tel");
		String addr = request.getParameter("addr");
		String etc = request.getParameter("etc");
		
		crb.setAdmin_id(admin_id);
		crb.setCust_nm(cust_nm);
		crb.setCeo_nm(ceo_nm);
		crb.setCorp_no(corp_no);
		crb.setUser_tel(cust_tel);
		crb.setCorp_addr(addr);
		crb.setEtc(etc);
		
		boolean result=false;
		boolean isExist=true;
		try{
			 isExist= dao.isExistCust(crb);
			if(isExist==false){
				result=dao.registCust(crb);
			}
			if(isExist==true)request.setAttribute("isExist", "Y");
			
		}catch(Exception e){
			result=false;
			e.printStackTrace();
		}
		
		request.setAttribute("result", isExist==false&&result==true?"Y":"N");
		request.setAttribute("cust_nm", crb.getCust_nm());
		request.setAttribute("corp_no", crb.getCorp_no());
		
		af.setRedirect(false);
		af.setPath("/admin/item/cust_reg.jsp");
		dao.close();
		return af;
	}

}
